package com.aqumon.www.controller;


import com.alibaba.fastjson.JSONObject;
import com.aqumon.www.entity.request.scripts.ScriptCreateReq;
import com.aqumon.www.entity.request.scripts.ScriptDelReq;
import com.aqumon.www.entity.request.scripts.ScriptEditReq;
import com.aqumon.www.entity.response.controller.Response;
import com.aqumon.www.service.ScriptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * jenkins job 列表 前端控制器
 * </p>
 *
 * @author aqumon
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/api/script")
public class ScriptController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptController.class);

    @Resource
    ScriptService scriptService;

    /**
     * 添加脚本（仅平台数据库添加，job需要在jenkins平台事先创建）
     */
    @PostMapping(value = "/createScript")
    public Response<?> createScript(@RequestBody ScriptCreateReq scriptCreateReq)  {
        LOGGER.info("添加脚本" + JSONObject.toJSONString(scriptCreateReq));
        return Response.success(scriptService.createScript(scriptCreateReq));
    }

    /**
     * 编辑脚本属性（仅QA平台属性）
     */
    @PostMapping(value = "/editScript")
    public Response<?>  editScript(@RequestBody ScriptEditReq scriptEditReq)  {
        LOGGER.info("编辑脚本属性" + JSONObject.toJSONString(scriptEditReq));
        return Response.success(scriptService.editScript(scriptEditReq));
    }

    /**
     * 删除脚本（仅平台数据库删除）
     */
    @DeleteMapping (value = "/deleteScript")
    public Response<?>  deleteScript(@RequestBody ScriptDelReq scriptDelReq) {
        LOGGER.info("删除脚本：" + JSONObject.toJSONString(scriptDelReq));
        return Response.success(scriptService.deleteScript(scriptDelReq));
    }

    @GetMapping(value = "/listAllScripts")
    public Response<?> listAllScripts(@RequestParam(required = false) Long jenkinsId)  {
        return Response.success(scriptService.listAllScripts(jenkinsId));
    }
}
